package com.example.administrator.shihuo.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.shihuo.Modle.ShaiWuModle;
import com.example.administrator.shihuo.R;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;

import java.util.List;

/**
 * Created by Sunshine on 2016/5/10.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    private List<ShaiWuModle.DataBean.ListBean> Data;
    private LayoutInflater inflater;

    public RecyclerAdapter(Context context, List<ShaiWuModle.DataBean.ListBean> mData) {
        this.Data = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycler_layout, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        OkHttpUtils.get().url(Data.get(position).getFront_pic()).tag("image").build().execute(new BitmapCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(Bitmap response) {
                if (response != null) {
                    holder.frontpicImageView.setImageBitmap(response);
                }
            }
        });
        OkHttpUtils.get().url(Data.get(position).getAuthor_avatar()).tag("image").build().execute(new BitmapCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(Bitmap response) {
                holder.authoravatarImageView.setImageBitmap(response);
            }
        });
        holder.authornameImageView.setText(Data.get(position).getTitle());
        holder.publishtimeTextView.setText(Data.get(position).getPublish_time());
        holder.titleTextView.setText(Data.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        return Data == null ? 0 : Data.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView frontpicImageView;
        ImageView authoravatarImageView;
        TextView authornameImageView;
        TextView publishtimeTextView;
        TextView titleTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            frontpicImageView = (ImageView) itemView.findViewById(R.id.iv_recycler_front_pic);
            authoravatarImageView = (ImageView) itemView.findViewById(R.id.iv_recycler_author_avatar);
            authornameImageView = (TextView) itemView.findViewById(R.id.tv_recycler_author_name);
            publishtimeTextView = (TextView) itemView.findViewById(R.id.tv_recycler_publish_time);
            titleTextView = (TextView) itemView.findViewById(R.id.tv_recycler_title);
        }
    }
}
